package cpr;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;

import cpr.data.HLVector;

public class HLSerializer {
    public final static String SPACE = " ";
    public final static String NR = "\n";
    
    private List rules = null;
    public HLSerializer (List rules) {
        this.rules = rules;
    }
    public String toStringHLVector(HLVector vector) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < vector.values.length; i++) {
            sb.append(vector.values[i]+SPACE);
        }
        return sb.toString();
        
    }
    public HLVector fromStringHLVector(String str) {
        StringTokenizer st = new StringTokenizer(str, SPACE);
        int[] values= new int[st.countTokens()];
        int i = 0;
        while (st.hasMoreElements()) {
            String e = st.nextToken();
            values[i] = Integer.parseInt(e);
            i++;
        }
        HLVector vector = new HLVector(values);
        return vector;
    }
    
    public int toStringHLRuleNr(Rule rule) {
        return rule.ruleNr;
    }
    public String  toStringRuleArray(List plus) {
        StringBuffer sb = new StringBuffer();
        for (Iterator iter = plus.iterator(); iter.hasNext();) {
            Rule rule = (Rule)iter.next();
            sb.append(rule.ruleNr+SPACE);
        }
        return sb.toString();
    }
    
    public List fromStringRuleArray(String str) {
        StringTokenizer st = new StringTokenizer(str, SPACE);
        
        List arr= new ArrayList(st.countTokens());
        while (st.hasMoreElements()) {
            int ruleNr = Integer.parseInt((String)st.nextElement());
            int rid = Utils.findRule(ruleNr, rules);
            if (rid==-1) {
                throw new RuntimeException("rule "+ruleNr+" not found");
            }
            Rule rule = (Rule) rules.get(rid);
            arr.add(rule);
        }
        return arr;
    }
    
    
//    public String toStringAR(AR ar) {
//        StringBuffer sb = new StringBuffer();
//        int arRuleNr = toStringHLRuleNr(ar.rule);
//        sb.append(arRuleNr+NR);
//        sb.append(ar.entriesMap.keySet().size()+NR);
//        for (Iterator emIter = ar.entriesMap.keySet().iterator(); emIter.hasNext();) {
//            String emKey = (String) emIter.next();
//            AREntry are = (AREntry )ar.entriesMap.get(emKey);
//            String ares = toStringAREntry(are);
//            sb.append(ares+NR);
//        }
//        sb.append(NR);
//        return sb.toString();
//    }
    
    public int readInt(BufferedReader br) throws IOException{
        String str = br.readLine();
        int i = Integer.parseInt(str);
        return i;
    }
//    public AR fromStringAR(BufferedReader br) throws IOException{
//        int ruleNr = Integer.parseInt(br.readLine());
//        int rid = Utils.findRule(ruleNr, rules);
//        Rule rule = (Rule)rules.get(rid);
//        
//        int entriesNum = readInt(br);
//        AR ar = new AR();
//        ar.rule = rule;
//        ar.entriesMap =  new HashMap();
//        
//        for (int i = 0; i < entriesNum; i++) {
//            AREntry are = fromStringAREntry(rule, br);
//            String areKey = Utils.aREntryPlusMinusString(are);
//            ar.entriesMap.put(areKey, are);
//            String nr = br.readLine();
//        }
//        String nr = br.readLine();
//        return ar;
//    }
    
//    public String toStringAREntry(AREntry are) {
//        StringBuffer sb = new StringBuffer();
//        
//        String p = null;
//        p = toStringHLVector(are.vector);
//        sb.append(p+NR);
//        p = toStringRuleArray(are.plus);
//        sb.append(p+NR);
//        p = toStringRuleArray(are.minus);
//        sb.append(p+NR);
//        return sb.toString();
//    }
//    
//    public AREntry fromStringAREntry(Rule rule, BufferedReader br) throws IOException{
//        String vector=br.readLine();
//        String plus=br.readLine();
//        String minus=br.readLine();
//        AREntry are = new AREntry();
//        are.rule = rule;
//        are.vector = fromStringHLVector(vector);
//        are.plus =  fromStringRuleArray(plus);
//        are.minus =  fromStringRuleArray(minus);
//        return are;
//    }
    
}
